package com.estudy.array;

import org.junit.Test;

/**
 * @author tim.xie
 * @date 2022/6/30 15:06
 * @description 53. 最大子数组和 https://leetcode.cn/problems/maximum-subarray/
 * 数据结构 数组 简单
 */
public class MaximumSubarray {

    public int maxSubArray(int[] nums) {
        int pre = 0, maxAns = nums[0];
        for (int x : nums) {
            pre = Math.max(pre + x, x);
            maxAns = Math.max(maxAns, pre);
        }
        return maxAns;
    }

    @Test
    public void test() {
        System.out.println(maxSubArray(new int[]{5,4,-1,7,8}));
        System.out.println(maxSubArray(new int[]{-2,1,-3,4,-1,2,1,-5,4}));
        System.out.println(maxSubArray(new int[]{-1, 0, -2}));
    }

}
